//
//  YUIPageControl.m
//  yuikemall
//
//  Created by yuike6 yuike6 on 12-2-15.
//  Copyright (c) 2012年 yuike. All rights reserved.
//

#import "YUIPageControl.h"
#import "header.h"

@interface YUIPageControl (private)
-(void) updateDots;
@end

@implementation YUIPageControl
@synthesize imagePageStateNormal = imagePageStateNormal_;
@synthesize imagePageStateHighlighted = imagePageStateHighlighted_;

- (void) dealloc
{
    YRelease(imagePageStateNormal_);
    YRelease(imagePageStateHighlighted_);
    
    [super dealloc];
}


- (void)setImagePageStateNormal:(UIImage*)image {  
    
    YRelease(imagePageStateNormal_);
    imagePageStateNormal_ = [image retain];
    
    [self updateDots];
}

-(void)setImagePageStateHighlighted:(UIImage *)image { 
    
    YRelease(imagePageStateHighlighted_);
    imagePageStateHighlighted_ = [image retain];
    
    [self updateDots];
}

- (CGFloat) originX
{
    for (UIImageView * subView in self.subviews) {
        return subView.frame.origin.x;
    }
    
    return 0.0f;
}

-(CGFloat) endY
{
    for (int i = [self.subviews count] ; i > 0; i--)
    {
        if (i<=0) {
            return 0.0f;
        }
        UIImageView * imageView = [self.subviews objectAtIndex:i - 1];
        
        return imageView.frame.origin.x + imageView.frame.size.width;
    }
     return 0.0f;
}


-(void) updateDots
{    
    int index = 0;
    for (UIImageView * subView in self.subviews) {
        [subView setImage:self.currentPage == index ? self.imagePageStateHighlighted : self.imagePageStateNormal];
        
        [subView setFrame:CGRectMake(subView.frame.origin.x, subView.frame.origin.y, self.imagePageStateNormal.size.width, self.imagePageStateNormal.size.width)];
        index ++;
    }
}

- (void) init_
{
    /*
     pageController.center = CGPointMake(self.view.frame.size.width/2, self.view.frame.size.height-15);//设置pageControl的位置
     pageController.numberOfPages = pagesCount;
     pageController.currentPage = 0;
     
     [pageController setBounds:CGRectMake(0, 0, 16 * (pagesCount - 1) + 16, 16)];// pageControl上的圆点间距基本在16左右
     [pageController.layer setCornerRadius:8];
     [pageController setBackgroundColor:[UIColor colorWithWhite:0.0 alpha:0.2]];
     */
    [self addTarget:self action:@selector(upevent) forControlEvents:UIControlEventTouchUpInside];
}

- (void) upevent
{ 
   [self updateDots];
}

-(void) setCurrentPage:(NSInteger)page
{
    [super setCurrentPage:page];
    [self updateDots];
}

- (CGSize) needDisplaySize
{
    CGSize size;
    size.height = self.imagePageStateNormal.size.height;
    size.width = self.imagePageStateNormal.size.width * (self.numberOfPages * 2)  ;
    return size;
}

- (id) init
{
    self = [super init];
    if (self) {
        [self init_];
    }
    return self;
}
@end
